import requests
from bs4 import BeautifulSoup

# 目标URL：2020年中国大学排名页面
url="http://www.shanghairanking.cn/rankings/bcur/2020"
response=requests.get(url)
response.encoding="utf-8"

soup=BeautifulSoup(response.text,"html.parser")

# 查找所有大学数据行：通过特定的data属性定位包含大学信息的表格行
university_rows = soup.find_all('tr', attrs={'data-v-389300f0': ''})

print("排名 学校名称\t\t省市\t类型\t总分")
print("-"*50)

#提取排名，名称，省市，类型，总分
for row in university_rows:
    rank_div=row.find("div", class_= "ranking")
    if rank_div:
        cells = row.find_all('td')
        # print(cells)
        if len(cells)>=5:
            rank=rank_div.get_text().strip()

            name_cn=cells[1].find("span", class_="name-cn")
            university = name_cn.get_text().strip() if name_cn else "未知"

            province = cells[2].get_text().strip()
            school_type = cells[3].get_text().strip()
            score_school = cells[4].get_text().strip()

            print(f"{rank:>2} {university:12} {province:6} {school_type:4} {score_school:4}")




